import { Component, OnInit } from '@angular/core';
import {
    FormBuilder,
    FormGroup,
    FormControl,
    Validators
} from '@angular/forms';
import { ApiService } from '../../../../providers/ApiPro.service';
import { NzModalService } from 'ng-zorro-antd';
import { ActivatedRoute, Router, RouterLink, RouterModule } from '@angular/router';


@Component({
  selector: 'app-ResOilcardDia',
  templateUrl: './ResOilcardDia.component.html',
  styleUrls: ['./ResOilcardDia.component.scss']
})
export class ResOilcardDiaComponent implements OnInit {
   validateForm: FormGroup;
   public isAdd: boolean = false;
   public oilCardId:number;
   public type:number;//--1添加--2查看--3修改
   public view:any = [];

  constructor(
    private fb: FormBuilder,
    public apiService: ApiService,
    private route: ActivatedRoute,
    private router: Router,
    private confirmServ: NzModalService
  ) { }

  ngOnInit() {
     this.validateForm = this.fb.group({
      oilCardId        : [ null, [ Validators.required ] ],
      oilCardNo        : [ null, [ Validators.required ] ],
      deadLine         : [ null, [ Validators.required ] ],
      oilCardStatus    : [ null, [ Validators.required ] ],
      remark           : [ null, [ Validators.required ] ]
    });
    this.getId();
  }

  _submitForm() {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[ i ].markAsDirty();
      }
    }
  getFormControl(name) {
    return this.validateForm.controls[ name ];
  }

  getId(){
    this.route.params.subscribe((params) => {
      switch(params['type']){
        case 'add':
        this.type = 1;
        this.isAdd = true;
        break;
        case 'view':
        this.type = 2;
        this.oilCardId = params['id'];
        this.getOilCardDetail();
        break;
        case 'modify':
        this.type = 3;
        this.oilCardId = params['id'];
        this.getOilCardDetail();
        break;
        default :
        break;
      }
       });
 }

 getOilCardDetail(){
  let url = "findoilcarddetail";
  this.apiService.get(url, { "oilCardId": this.oilCardId })
    .then(res => {
      this.validateForm = this.fb.group({
        oilCardId        : [ res.result.oilCardId, [ Validators.required ] ],
        oilCardNo        : [ res.result.oilCardNo, [ Validators.required ] ],
        deadLine         : [ res.result.deadLine, [ Validators.required ] ],
        oilCardStatus    : [ res.result.oilCardStatus, [ Validators.required ] ],
        remark           : [ res.result.remark, [ Validators.required ] ]
      });
      this.view = res.result;
      console.log(this.validateForm.value);
      console.log(this.validateForm.value.oilCardStatus);
    })
}


  createNew(){
    this._submitForm();
    console.log(this.validateForm.value);
    this.validateForm.value.deadLine = this.apiService.formatDateTime2(this.validateForm.value.deadLine);
    if(this.isAdd){
      let url = "addoilcards";
      this.apiService.post(url,this.validateForm.value)
        .then(res =>{
          console.log(res);
          if(res.result){
            this.success();          
          }else{
            this.error();
          }
        })
    }else{
      let url = "editoilcards";
      this.validateForm.value.deadLine = this.apiService.formatDateTime2(this.validateForm.value.deadLine);
      this.apiService.post(url,this.validateForm.value)
        .then(res =>{
          console.log(res);
          if(res.result){
            this.success();          
          }else{
            this.error();
          }
        })
    }
  }

  error() {
    this.confirmServ.error({
      title: '错误',
      content: '输入的信息有误或已生成信息',
      okText:'返回'
    });
  }

  success(){
    let self = this;
      this.confirmServ.success({
      title: '成功',
      content: '即将返回油卡管理页面',
      okText:'返回',
      onOk() {
        self.router.navigateByUrl("layout/res/oil");
      },
    });
  }
}
